package com.liuanbang.saro.sarodemo.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageButton;

import com.jaeger.library.StatusBarUtil;
import com.liuanbang.saro.sarodemo.R;
import com.liuanbang.saro.sarodemo.adapter.RecyclerAdapter;
import com.liuanbang.saro.sarodemo.base.BaseActivity;
import com.liuanbang.saro.sarodemo.ui.items.zxing.CaptureActivity;
import com.liuanbang.saro.sarodemo.util.HidingScrollListener;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Random;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 这里使用了 悬浮球
 */
public class MainActivity extends BaseActivity {
    @Bind(R.id.toolbar_main)
    Toolbar toolbarMain;
    @Bind(R.id.recyclerView_main)
    RecyclerView recyclerViewMain;
    @Bind(R.id.fabButton_main)
    ImageButton fabButtonMain;

    private int mColor;
    private Context mContext = this;
    private List<Class> list_activity = new ArrayList<>();
    //有序map
    private LinkedHashMap<String, Class> map = new LinkedHashMap<>();

    @Override
    protected void initView(Bundle savedInstanceState) {
        //主题使用的方法
        setTheme(R.style.AppTheme);
        //这里是BaseActivity的工具类的使用方法
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initDate();
        initToolbar();
        initRecyclerView();

        //注意啊!!!!要使用    public void onClick(View v) 必须先写这个方法
        fabButtonMain.setOnClickListener(this);

    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    @Override
    public Toolbar getToolbar() {
        return null;
    }

    @Override
    public void onClick(View v) {
        showToast(v.getId() + "");
        switch (v.getId()) {
            case R.id.fabButton_main:
                showToast("换颜色");
                Random random = new Random();
                mColor = 0xff000000 | random.nextInt(0xffffff);
                toolbarMain.setBackgroundColor(mColor);
                StatusBarUtil.setColor(this, mColor, 0);
                break;
            default:
                break;
        }
    }

    /**
     * 初始化RecycleView
     */
    private void initRecyclerView() {
        recyclerViewMain.setLayoutManager(new LinearLayoutManager(this));
        RecyclerAdapter adapter = new RecyclerAdapter(createItemList(), recyclerViewMain);
        recyclerViewMain.setAdapter(adapter);
        //recycleView的滚动状态监听,判断RecycleView 上下  来进行隐藏Toolbar,只在需要显示的内容超出屏幕范围以外后起作用
        recyclerViewMain.addOnScrollListener(new HidingScrollListener() {
            @Override
            public void onHide() {
                hideViews();
            }

            @Override
            public void onShow() {
                showViews();
            }
        });

        //recycleView的监听器
        adapter.setOnRecycleViewItemClickListener(new RecyclerAdapter.OnRecycleViewItemClickListener() {
            @Override
            public void onItemClick(int position) {
                //   showToast(position + "");
                startActivity(new Intent(mContext, list_activity.get(position - 1)));
            }
        });
    }

    private void initToolbar() {
        setSupportActionBar(toolbarMain);
        setTitle("就是随便写写,别当真");
        toolbarMain.setTitleTextColor(ContextCompat.getColor(this, android.R.color.white));
    }

    private void hideViews() {
        toolbarMain.animate().translationY(-toolbarMain.getHeight()).setInterpolator(new AccelerateInterpolator(2));

        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) fabButtonMain.getLayoutParams();
        int fabBottomMargin = lp.bottomMargin;
        fabButtonMain.animate().translationY(fabButtonMain.getHeight() + fabBottomMargin).setInterpolator(new AccelerateInterpolator(2)).start();
    }

    private void showViews() {
        toolbarMain.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2));
        fabButtonMain.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
    }

    /**
     * 处理 数据源
     *
     * @return
     */
    private List<String> createItemList() {
        List<String> itemList = new ArrayList<>();
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            itemList.add(key);
            list_activity.add(map.get(key));
        }
        return itemList;
    }

    /**
     * 跳转Activity数据源
     */
    private void initDate() {
        map.put("二维码扫描", CaptureActivity.class);
        map.put("RecycleView", TestActivity.class);
        map.put("ToolBar", ToolBarActivity.class);
        map.put("Toast/Dialog/Snackbar", ToastDialogSanckbarActivity.class);
        map.put("DownloadManager", DownloadManagerActivity.class);
        map.put("动态更换状态布局", StatusLayoutActivity.class);
        map.put("各种自定义控件使用", ViewsActivity.class);
        map.put("注解使用", AnnotationDemoActivity.class);
        map.put("Notification", NotificationActivity.class);
        map.put("禁止截图", CanNotGetScreenActivity.class);
        map.put("提示框", TipActivity.class);
        map.put("浮动Button,解决方案", CustSocialSecurityActivity.class);
        map.put("广告栏轮播", ViewFlipperActivity.class);
        map.put("时间选择", TimeSelectorActivity.class);
    }
}
